import java.util.HashSet;

/**
 * @Auther: wangliuyang
 * @Date: 2021/4/16 21:12
 * @Description: 给定一个字符串，请你找出其中不含有重复字符的 最长子串的长度。
 */
public class Solution_3 {
    public int lengthOfLongestSubstring(String s) {
        int left, right, ans;
        ans = left = right = 0;
        HashSet<Character> set = new HashSet<>();
        while (right < s.length()) {
            char c = s.charAt(right);
            right++;
            if (!set.contains(c)) {
                set.add(c);
                ans = Math.max(ans, right - left);
            } else {
                //此时窗口收缩
                while (set.contains(c)) {
                    set.remove(s.charAt(left));
                    left++;
                }
                set.add(c);
            }
        }
        return ans;
    }

}
